package com.yx.shop.web.admin;

import com.yx.shop.utils.FileUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.util.Collection;

/**
 * @Classname ${NAME}
 * @Description TODO:
 * @Date 2021/04/22 11:35
 * @Created by yx
 */
@WebServlet(name = "MoreFileUpload",value = "/moreUpload")
@MultipartConfig(maxFileSize = 1024*1024*100,maxRequestSize = 1024*1024*200)
public class MoreFileUpload extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String savePath = request.getServletContext().getRealPath("/WEB-INF/upload");
        File file = new File(savePath);
        if (!file.exists()){
            file.mkdir();
        }
        Collection<Part> parts = request.getParts();
        for (Part part : parts) {
            String fileName = part.getSubmittedFileName();
            if (fileName!=null) {
                if (fileName.length()>0){
                    String newName = FileUtil.newFileName(fileName);
                    String newPath = FileUtil.newFilePath(fileName,savePath);
                    part.write(newPath+File.separator+newName);
                    response.getWriter().println(fileName+"&nbsp上传成功!");
                }
            } else {
                String pname = request.getParameter("pname");
                String tid = request.getParameter("goodsParent");
                String ptime = request.getParameter("ptime");
                String pprice = request.getParameter("pprice");
                String pstate = request.getParameter("pstate");
                String pinfo = request.getParameter("pinfo");

            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
